package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.domain.chronicdis.Chronicdis;
import com.gxuwz.medical.domain.chronicinfo.Chronicinfo;
import com.gxuwz.medical.domain.family.FamilyMember;

/**
 * 慢性病档案管理控制模块
 * @author 演示
 *
 */
public  class ChronicInfoServlet extends BaseServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String m = req.getParameter("m");// 请求处理动作类型:list:显示列表；get：根据ID读取记录;input:新增数据;add:保存新记录；edit：更新记录
		String url = ERROR_PATH;
		try {
			if ("list".equals(m)) {	
				url="/page/chronicinfo/chronicinfo_list.jsp";
			} else if ("input".equals(m)) {
				url="/page/chronicinfo/chronicinfo_add.jsp";
			} else if("edit".equals(m)){
				this.edit(req, resp);
			} else if ("add".equals(m)) {
				url = this.add(req, resp);
			}
			   process(req, resp, url);
		} catch (Exception e) {
			e.printStackTrace();
		}
		  

	}
	/**
	 * 添加慢病档案
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	private String add(HttpServletRequest req,HttpServletResponse resp)throws Exception{
		 //1:获取慢病档案信息
		 String perscode=req.getParameter("perscode");
		 String illcode=req.getParameter("illcode");
		 String starttime=req.getParameter("starttime");
		 String endtime=req.getParameter("endtime");
		 //从session对象中获取当前用户名称
		 String creator=(String)req.getSession().getAttribute("userid");
		//2:实例化慢病证管理模块组件
		 Chronicinfo model=new Chronicinfo();
		 //3:将请求参数赋值给实例的成员属性
		 model.setMembercode(new FamilyMember(perscode));
		 model.setIllcode(new Chronicdis(illcode));
		 model.setStarttime(starttime);//慢病开始时间
		 model.setEndtime(endtime);//慢病结束时间
		 model.setCreator(creator);//建档人
		 model.setStatus("正常");//档案状态：1表示正常
		 model.setCreattime(new Date());
		 model.addChroInfo();
		 return "/page/chronicinfo/chronicinfo_list.jsp";
		
	}	
	
	private void edit(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{

		 //1:获取慢病档案信息
		 String perscode=req.getParameter("perscode");
		 String illcode=req.getParameter("illcode");
		 String starttime=req.getParameter("starttime");
		 String endtime=req.getParameter("endtime");
	  	  //2:构造新慢病信息对象
		 Chronicinfo model=new Chronicinfo();
	  	  //3：调用保存的方法
	  	  try{
	  		  model.edit();
	  		process(req, resp, "/page/chronicinfo/chronicinfo_list.jsp");
	  	  }catch(Exception e){
	  		  error(req, resp);
	  	  }
		
	  
		}

}
